package com.xxx;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Set;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/20  9:21
 * @Copyright
 */
public class TimeDemo01 {
    public static void main(String[] args) {
        //now();
        //of();
        //plus()
        //mialli();
       // Zone();
        //fromat();
        //periods();//计算年月日
        //Drations();//计算时分秒
       // chronouit();

        //Date --> LocaDateTime
        Date date = new Date();
        System.out.println("date = " + date);
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        System.out.println("localDateTime = " + localDateTime);

        //LocaDateTime --> Instant-->  Date
        Instant instant1 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date1 = Date.from(instant1);



    }

    private static void chronouit() {
        LocalDate endDate1 = LocalDate.now();
        LocalDate startDate = LocalDate.of(2008, 8, 8);
        long l = ChronoUnit.DAYS.between(startDate, endDate1);
        System.out.println("l = " + l);
    }

    private static void Drations() {
        Instant now = Instant.now();
        Instant instant = now.plusSeconds(10);
        long l = Duration.between(now, instant).toMillis();//计算时分秒
        System.out.println("l = " + l);
    }

    private static void periods() {
        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.of(2008, Month.AUGUST, 8);
        Period between = Period.between(start, end);
        System.out.println(between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");
    }

    private static void fromat() {
        //格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println("format = " + format);
    }

    private static void Zone() {
        //时区
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        for (String availableZoneId : availableZoneIds) {
            System.out.println("availableZoneId = " + availableZoneId);
        }
        String s = ZoneId.systemDefault().toString();
        System.out.println("s = " + s);

        ZonedDateTime now = ZonedDateTime.now();
        System.out.println("now = " + now);

        LocalDateTime now1 = LocalDateTime.now();
        System.out.println("now1 = " + now1);
        now1.atZone(ZoneId.of(" Asia/Shanghai") );//指定时区
    }

    private static void mialli() {
        //毫秒值
        Instant in = Instant.now();
        long l = in.toEpochMilli();
        System.out.println(in.toString());
        System.out.println("l = " + l);
    }

    private static void plus() {
        //LocalDate
        LocalDate localDate = LocalDate.now();//当前日期
        System.out.println("localDate = " + localDate);
        LocalDate localDate1 = localDate.plusYears(-5).plusDays(-20);//加或减年数
        LocalDate localDate2 = localDate.minusDays(-100);//减或加去天数
        System.out.println("localDate2 = " + localDate2);
        System.out.println("localDate1 = " + localDate1);

        //LocalTime
        LocalTime localTime = LocalTime.now();//当前时间
        System.out.println("localTime = " + localTime);
        LocalTime localTime1 = localTime.plusHours(-10);//加或减
        System.out.println("localTime1 = " + localTime1);
    }

    private static void of() {
        //指定时间
        LocalDate localDate = LocalDate.of(2018,8,8);
        System.out.println("localDate = " + localDate);
        LocalTime localTime = LocalTime.of(12,20,20);
        System.out.println("localTime = " + localTime);
        LocalDateTime localDateTime = LocalDateTime.of(2020,12,13,14,36,45);
        System.out.println("localDateTime = " + localDateTime);
    }

    private static void now() {
        Instant in = Instant.now();//时间戳
        System.out.println("in = " + in);
        LocalDate lo = LocalDate.now();//日期
        System.out.println("lo = " + lo);
        LocalTime lt = LocalTime.now();//时间
        System.out.println("lt = " + lt);
        LocalDateTime ldt = LocalDateTime.now();//日期时间
        System.out.println("ldt = " + ldt);
        ZonedDateTime zdt = ZonedDateTime.now();//时区
        System.out.println("zdt = " + zdt);
    }
}
